package com.jinlejia.bbs.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsAdapterRecommendItemBinding;

import java.util.ArrayList;

/**
 * 推荐列表适配器
 * @author 王进 on 2019/3/6.
 */

public class RecommendAdapter extends RecyclerView.Adapter{
    private Context context;
    private ArrayList<BBSUserBean> list;
    private RecommendListener listener;
    RecommendAdapter(Context context,RecommendListener listener){
        this.context = context;
        this.listener = listener;
    }

    public void setList(ArrayList<BBSUserBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.bbs_adapter_recommend_item, parent, false);
        return new RecommendItemViewHolder(view);
    }
    private int [] bgs = new int[]{R.drawable.bbs_recommend_bg,R.drawable.bbs_recommend_bg_01,R.drawable.bbs_recommend_bg_02};
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (list!=null){
            final BBSUserBean userBean = list.get(position);
            BbsAdapterRecommendItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
            int i = position % 3;
            binding.view.setBackgroundResource(bgs[i]);
            binding.setBean(userBean);
            binding.recommendLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onRecommend(userBean);
                    }else {

                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class RecommendItemViewHolder extends RecyclerView.ViewHolder{

        RecommendItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    interface RecommendListener{
        void onRecommend(BBSUserBean userBean);
    }
}
